package com.wenzheng.quartz.controller;

import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.*;

@Controller
public class SysController {
	private static final String CRON_PAGE = "cron";
	private static final String LOGIN_PAGE = "login";
	private static final String saas_url = "http://saas.3rdjx.com";


	@GetMapping("/")
	public String home(Model model, HttpServletRequest request) {
		request.getSession().setAttribute("login_user", "admin");
		// 耍了个小心机 ，url链接到我的博客
		request.getSession().setAttribute("saas_url", saas_url);
		return "redirect:/task/home";
	}

	@GetMapping("/showCron")
	public String showCron() {
		return CRON_PAGE;
	}

	// 跳登录页面
	@GetMapping("/login")
	public String login(HttpServletRequest request) {

		request.getSession().setAttribute("login_user", "admin");
		// 耍了个小心机 ，url链接到我的博客
		request.getSession().setAttribute("saas_url", saas_url);
		Object object = request.getSession().getAttribute("login_user");
		// 已在线的不能访问登录页面
		if (null == object) {
			return LOGIN_PAGE;
		}else {
			return "redirect:/task/home";
		}
	}

	// 登录
	@PostMapping("/toLogin")
	public String toLogin(String account, String password, HttpServletRequest request, Model model) {
		request.getSession().setAttribute("login_user", "admin");
		// 耍了个小心机 ，url链接到我的博客
		request.getSession().setAttribute("saas_url", saas_url);
		return "redirect:/task/home";
	}

	// 退出登录
	@GetMapping("/logout")
	public String logout(HttpServletRequest request) {
		// 清空session
		request.getSession().invalidate();
		// 重定向到登录页面
		return "redirect:/login";
	}
}
